//index.js
//获取应用实例
const app = getApp()

import {
  userLogin
} from "../../api/user.js";

Page({
  data: {
    username: '',
    password: ''
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    wx.hideTabBar({})
  },
  onLoad: function () {

  },

  register() {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },


  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  // 登录处理
  login: function () {
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      wx.showToast({
        title: '账号密码不为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      let param = {
        username: this.data.username,
        password: this.data.password
      }
      userLogin(param).then(res => {
        console.log(res);
        if (res.code !== '200') {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        } else if (res.code == '200') {
          wx.setStorage({
            key: "usersinfo",
            data: JSON.stringify(res.data)
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else {
          wx.showToast({
            title: "登录失败",
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  }
})